﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using QS.Logic.Objects;
using Microsoft.Xna.Framework.Graphics;

namespace QS.Game.Camera
{
    class QFirstPersonCamera : QCamera
    {
        private QSpaceShip spaceShip;
        private Vector3 relPosition;
        private Vector3 transformedRelPosition;

        public QFirstPersonCamera(GraphicsDevice device, QSpaceShip spaceShip)
            : base(device)
        {
            this.spaceShip = spaceShip;
            m_ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 480f, 0.01f, 1000f);
            relPosition = new Vector3(0,0.2f,-0.0f);
            updateLookAt();
        }

        public override void Update(GameTime time)
        {
            base.Update(time);
            updateLookAt();
        }

        private void updateLookAt()
        {
            transformedRelPosition = Vector3.Transform(relPosition, this.spaceShip.Orientation);
            m_ViewMatrix = Matrix.CreateLookAt(Position, this.spaceShip.Position + transformedRelPosition + Vector3.Transform(Vector3.Forward, this.spaceShip.Orientation), Vector3.Up);
        }

        public override Vector3 Position
        {
            get
            {
                return this.spaceShip.Position + transformedRelPosition;
            }
        }
    }
}
